package edu.ustb.efws.pipeline.controller;

import edu.ustb.efws.base.domain.Position;
import edu.ustb.efws.base.service.PositionService;
import edu.ustb.efws.controller.vo.ResultPageVo;
import edu.ustb.efws.controller.vo.ResultVo;
import edu.ustb.efws.pipeline.controller.vo.RiskParamVo;
import edu.ustb.efws.pipeline.domain.RiskLog;
import edu.ustb.efws.pipeline.service.RiskLogService;
import edu.ustb.efws.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 风险日志原始数据查询
 *
 * @author TS Group
 */
@RestController
@RequestMapping("/riskLog")
@Api(value = "/riskLog", tags = "风险日志原始数据")
public class RiskLogController {
    private final RiskLogService service;
    private final PositionService positionService;

    @Autowired
    public RiskLogController(RiskLogService service, PositionService positionService) {
        this.service = service;
        this.positionService = positionService;
    }

    @GetMapping(value = {"paras", "params"}, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取查询条件, 是Map数据类型")
    public ResultVo<Map<String, List<RiskParamVo>>> params(){
        List<Position> positions = positionService.all();
        Map<String, List<RiskParamVo>> map = new LinkedHashMap<>();

        for(Position position: positions){
            List<RiskParamVo> ps = map.computeIfAbsent(position.getRiskArea(), k -> new ArrayList<>());
            ps.add(new RiskParamVo(position.getId(), position.getRiskPosition()));
        }

        return ResultVo.success(map);
    }

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("查询原始数据")
    public ResultPageVo<RiskLog> query(
            @ApiParam(value = "风险点编号") @RequestParam int positionId,
            @ApiParam(value = "日期") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
            @RequestParam(defaultValue = "true") @ApiParam(value = "是否查询记录数") Boolean isCount,
            @RequestParam(defaultValue = "0") @ApiParam(value = "查询页数") int page,
            @RequestParam(defaultValue = "15") @ApiParam(value = "查询每页记录数") int rows){

        LocalDate localDate = DateUtils.toLocalDate(date);
        int month = DateUtils.localDateToMonthNum(localDate);
        Date from = DateUtils.toDate(localDate);
        Date to = DateUtils.toDate(localDate.plusDays(1));

        List<RiskLog> data = service.query(positionId, month, from, to, page * rows, rows).stream()
                .peek(e -> e.setScore(format(e.getScore()))).collect(Collectors.toList());

        return new ResultPageVo
                .Builder<>(page, rows, data)
                .count(isCount, () -> service.count(positionId, month, from, to))
                .build();
    }

    private float format(float f){
        return (Math.round(f * 100) * 1.0f)/100;
    }
}
